package cn.tianjindong.blog_portal.controller;

import java.util.List;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;

import cn.tianjindong.blog.common.jedis.JedisClient;
import cn.tianjindong.blog.common.pojo.PageView;
import cn.tianjindong.blog.common.utils.CookieUtils;
import cn.tianjindong.blog.pojo.BlogArticle;
import cn.tianjindong.blog.pojo.BlogArticleComment;
import cn.tianjindong.blog_portal.service.ArticleService;
import cn.tianjindong.blog_portal.service.CommentService;

/**
 * 文章相关Controller
 * 
 * @author 田 金 东
 *
 */
@Controller
public class ArticleController {

	@Autowired
	private ArticleService articleService;

	@Autowired
	private CommentService commentService;

	private static final String JSESSIONID = "JSESSIONID";

	/**
	 * 通过文章分类的别名和文章的id前往文章页面
	 * 
	 * @param model
	 * @param categoryAlias
	 * @param id
	 * @return
	 */
	@RequestMapping("/article/{alias}/{id}.html")
	public String toArticlePage(HttpServletRequest request, @PathVariable("alias") String categoryAlias,
			@PathVariable Long id) {
		// TODO
		// 记录文章访问次数未开发，结合Redis缓存机制，避免刷新操作计入访问次数中去（使用JSEESIONID作为缓存的Key，就可以避免刷新问题）
		// 获取JSESSIONID
		String jseesionid = CookieUtils.getCookieValue(request, JSESSIONID);
		BlogArticle article = articleService.getArticleByIdAndAlias(id, categoryAlias, jseesionid);
		if (article != null && article.getTab() == 1) {
			request.setAttribute("article", article);
			return "/article";
		}
		return "/404";
	}

	/**
	 * 通过文章的id前往文章页面（推荐使用上一种方法进入文章页面）
	 * 
	 * @param model
	 * @param categoryAlias
	 * @param id
	 * @return
	 */
	@RequestMapping("/article/{id}.html")
	public String toArticlePage(HttpServletRequest request, @PathVariable Long id) {
		// 获取JSESSIONID
		String jseesionid = CookieUtils.getCookieValue(request, JSESSIONID);
		BlogArticle article = articleService.getArticleById(id, jseesionid);
		if (article != null && article.getTab() == 1) {// 查询到文章并且文章是可访问的
			request.setAttribute("article", article);
			return "/article";
		}
		return "/404";
	}

	/**
	 * 处理前台/common/right.jsp中的ajax请求，获取指定个数的热门文章
	 * 
	 * @param countItem
	 * @return
	 */
	@RequestMapping("/article/hot-list.action")
	public @ResponseBody String toHotArticlePage(Integer countItem) {
		List<BlogArticle> list = articleService.getArticleListOrderByVisitNum(countItem);
		return JSON.toJSONString(list);
	}

	/**
	 * 处理首页加载时获取最新文章的请求
	 * 
	 * @param currentPage
	 * @param countItem
	 * @return
	 */
	@RequestMapping("/article/loadArticleLimit.action")
	public @ResponseBody String loadArticleList(@RequestParam(defaultValue = "1") Integer currentPage,
			@RequestParam(defaultValue = "5") Integer countItem) {
		// TODO 评论数未显示
		List<BlogArticle> pageView = articleService.getArticlePageView(currentPage, countItem);
		return JSON.toJSONString(pageView);
	}

	/**
	 * 获取当前文章的所有评论
	 * 
	 * @param currentPage
	 * @return
	 */
	@RequestMapping("/article/comment/list.action")
	public @ResponseBody String loadCommentList(Long articleId) {
		List<BlogArticleComment> list = commentService.getCommentListByArticleId(articleId);
		return JSON.toJSONString(list);
	}
}
